package com.ruoyi.statisticalCenter.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.InitTableVo;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.logistic.mapper.FlowReportMapper;
import com.ruoyi.statisticalCenter.domain.vo.ExcelScFlowReportDispositionVo;
import com.ruoyi.statisticalCenter.domain.vo.ScFlowReportDispositionVo;
import com.ruoyi.statisticalCenter.service.ScFlowReportDispositionService;
import com.ruoyi.statisticalCenter.mapper.ScFlowReportDispositionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流向报表业务层处理
 *
 *  @author cmz
 *  @date 2024-04-08
 */
@Service
public class ScFlowReportDispositionServiceImpl implements ScFlowReportDispositionService {
    @Resource
    private ScFlowReportDispositionMapper scFlowReportDispositionMapper;

    @Resource
    private FlowReportMapper flowReportMapper;

    @Resource
    private TableService tableService;


    @Override
    public R collect(Map<String, Object> map) {
        //获取配置参数
        ScFlowReportDispositionVo scFlowReportDispositionVo = scFlowReportDispositionMapper.select().get(0);

        map.put("daily_assessment_weight",scFlowReportDispositionVo.getDailyAssessmentWeight());
        map.put("assessment_standards",scFlowReportDispositionVo.getAssessmentStandards());
        map.put("avg_weight_index",scFlowReportDispositionVo.getAvgWeightIndex());
        map.put("avg_weight_assessment_standard",scFlowReportDispositionVo.getAvgWeightAssessmentStandard());
        map.put("flow_indicators",scFlowReportDispositionVo.getFlowIndicators());
        map.put("flow_assessment",scFlowReportDispositionVo.getFlowAssessment());

        List<Map<String, Object>> list = flowReportMapper.collect(map);

        List<TableVo> tableVos = new ArrayList<>();
        TableVo tableVo = new TableVo();
        tableVo.setFields("统计日期");
        tableVo.setTranslate("send_date");
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("网点/承包区");
        tableVo.setTranslate("network_area");
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("stop");
        tableVo.setTranslate("止损考核");
        List<TableVo> tableSunVos = new ArrayList<>();
        String[] stop = {"daily_check_weight", "stop_loss_weight", "settle_weight", "assessment_standards","stop_loss_check"};
        InitTableVo initTableVo = new InitTableVo();
        initTableVo.setFields(stop);
        initTableVo.setCode("flowReport");
        tableSunVos = tableService.initTable(initTableVo);
        tableVo.setSunList(tableSunVos);
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("weight");
        tableVo.setTranslate("均重考核");
        tableSunVos = new ArrayList<>();
        String[] weight = {"package_votes", "package_weight", "package_warehouse_avg_weight", "avg_weight_index","avg_weight_assessment_standard","avg_weight_check"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(weight);
        initTableVo.setCode("flowReport");
        tableSunVos = tableService.initTable(initTableVo);
        tableVo.setSunList(tableSunVos);
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("flow");
        tableVo.setTranslate("流向考核");
        tableSunVos = new ArrayList<>();
        String[] flow = {"ningxia_votes", "gansu_votes", "qinghai_votes", "hainan_votes","flow_votes_sum","flow_proportion","flow_index","flow_check"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(flow);
        initTableVo.setCode("flowReport");
        tableSunVos = tableService.initTable(initTableVo);
        tableVo.setSunList(tableSunVos);
        tableVos.add(tableVo);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", PageInfo.info(list));
        jsonObject.put("fields", tableVos);

        if (list == null || list.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(jsonObject);
        }
    }

    @Override
    public R export(String start_date, String end_date, String statistical_dimension, String contract_area, String path, String sheetName) {
        if (!path.contains(":")||!(path.endsWith(".xlsx")
                ||path.endsWith(".xls")
        )){
            return R.fail("设置的导出文件路径错误！！！");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("start_date", start_date);
        map.put("end_date", end_date);
        map.put("statistical_dimension", statistical_dimension);
        map.put("contract_area", contract_area);
        //获取配置参数
        ScFlowReportDispositionVo scFlowReportDispositionVo = scFlowReportDispositionMapper.select().get(0);
        map.put("daily_assessment_weight",scFlowReportDispositionVo.getDailyAssessmentWeight());
        map.put("assessment_standards",scFlowReportDispositionVo.getAssessmentStandards());
        map.put("avg_weight_index",scFlowReportDispositionVo.getAvgWeightIndex());
        map.put("avg_weight_assessment_standard",scFlowReportDispositionVo.getAvgWeightAssessmentStandard());
        map.put("flow_indicators",scFlowReportDispositionVo.getFlowIndicators());
        map.put("flow_assessment",scFlowReportDispositionVo.getFlowAssessment());

        sheetName = sheetName==null||sheetName.isEmpty()?"数据1":sheetName;

        List<ExcelScFlowReportDispositionVo> lise = flowReportMapper.flowReprotExcelVo(map);
        EasyExcel.write(path, ExcelScFlowReportDispositionVo.class).sheet(sheetName).doWrite(lise);
        return R.ok();
    }


    @Override
    public R updateDisposition(ScFlowReportDispositionVo scWeightFlowDirectionDispositionVo) {
        scWeightFlowDirectionDispositionVo.setId(1);
        return scFlowReportDispositionMapper.updateById(scWeightFlowDirectionDispositionVo,scWeightFlowDirectionDispositionVo.getId()) > 0 ? R.ok("修改成功") : R.fail("修改失败");
    }

    @Override
    public R selectDisposition() {
        return R.ok(scFlowReportDispositionMapper.select());
    }

}
